package com.fs.web.base.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.web.base.utils.PageHeper;
import com.fs.web.base.vo.Sort;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author ACE
 * @desc 公用方法service
 * @creationTime 2019-04-08 15:40
 * @projectName gps-x
 * @packageName com.fs.web.base.service
 */
@Slf4j
@Service
public class BaseService {
    @Autowired
    private ApplicationContext ctx;

    public <K> BaseMapper<K> beanSpecification(String className){
        BaseMapper<K> T = null;
        try {
            className = beanNameFormat(className);
            T = getBean(className);
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }
        return T;
    }

    public <K> BaseMapper<K> beanSpecification(Class<?> class1){
        return beanSpecification(class1.toString());
    }

    public <K> List<Object> findAllWithSort(String className, HttpServletRequest request, QueryWrapper<Object> spec){
        List<Object> list = null;
        try {
            /*Sort sort = PageHeper.sort(request);
            if(sort!=null){
                if(StringUtils.isBlank(sort.getType()) || "asc".equals(sort.getType())){
                    for (String s : sort.getField()) {
                        spec.orderByAsc(s);
                    }
                }else{
                    for (String s : sort.getField()) {
                        spec.orderByDesc(s);
                    }
                }
            }*/
            orderBy(request,spec);
            list = beanSpecification(className).selectList(spec);
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }
        return list;
    }
    public <K> List<Object> findAll(String className,QueryWrapper<Object> spec){
        List<Object> list = null;
        try {
            list = beanSpecification(className).selectList(spec);
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }
        return list;
    }

    public IPage page(String className, HttpServletRequest request, QueryWrapper<Object> spec){
        IPage page = null;
        try {
            Class<?> name = Class.forName(className);
            orderBy(request,spec);
            Page pageRequest = PageHeper.pageRequest(request, name);
            page = beanSpecification(className).selectPage(pageRequest, spec);
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }
        return page;
    }

    /**
     * 取spring容器中的服务如：menuMapper,roleMapper...
     * @param className
     * @return
     */
    private static String beanNameFormat(String className){
        if(className==null) { return null; }
        Integer lastIndex = className.lastIndexOf(".");
        className = className.substring(++lastIndex);
        className = className.substring(0, 1).toLowerCase().concat(className.substring(1)).concat("Mapper");
        return className;
    }

    @SuppressWarnings("unchecked")
    public <Bean> Bean getBean(String beanName) {
        return ctx != null ? (Bean)ctx.getBean(beanName) : null;
    }

    private void orderBy(HttpServletRequest request, QueryWrapper<Object> spec){
        Sort sort = PageHeper.sort(request);
        if(sort!=null){
            if(StringUtils.isBlank(sort.getType()) || "asc".equals(sort.getType())){
                for (String s : sort.getField()) {
                    spec.orderByAsc(s);
                }
            }else{
                for (String s : sort.getField()) {
                    spec.orderByDesc(s);
                }
            }
        }
    }
}
